﻿<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<meta http-equiv="Content-Language" content="ch-cn">
<title>创建试卷模板</title>
<script type="text/javascript" src="json2.js"></script>

<script language="javascript">
	function detail_problem()
	{
		var subject = document.model.subject.value;
		var total_problems = document.model.total_problems.value;
		var total_score = document.model.total_score.value;
		if (isNaN(total_problems))
		{
			alert("大题总数应为数字！请重新输入。");
			document.model.total_problems.value = "";
		} else if (isNaN(total_score))
		{
			alert("大题总分应为数字！请重新输入。");
			document.model.total_score.value = "";
		} else if ((subject != "") && (total_problems != "")
				&& (total_score != ""))
		{
			var id = "list";
			var partnum = document.getElementById(id).getElementsByTagName(
					"part_div").length;
			for ( var i = 1; i <= partnum; i++)
			{
				document.getElementById(id).removeChild(
						document.getElementById("type" + i).parentNode);
			}

			for ( var i = 1; i <= total_problems; i++)
			{
				var newdiv = document.createElement("part_div");
				newdiv.id = "part_div" + i;

				var parttip = document.createElement("span");//往页面动态插入html元素的方法!!
				parttip.id = "parttip" + i;
				parttip.innerHTML = "第" + i + "大题：";
				parttip.style.display = "block";

				var type = document.createTextNode("题型：");
				var inputTxt1 = document.createElement("input");
				inputTxt1.type = "text";
				inputTxt1.name = "type";
				inputTxt1.id = "type" + i;
				inputTxt1.onchange = function()
				{
					score_distribute(this.id.substring(4, 6));//传i
				};//传数字，即第几大题

				var tol_num = document.createTextNode("题数：");
				var inputTxt2 = document.createElement("input");
				inputTxt2.type = "text";
				inputTxt2.name = "tol_num";
				inputTxt2.id = "tol_num" + i;
				inputTxt2.onchange = function()
				{
					score_distribute(this.id.substring(7, 9));//传i
				};

				var tol_score = document.createTextNode("总分值：");
				var inputTxt3 = document.createElement("input");
				inputTxt3.type = "text";
				inputTxt3.name = "tol_score";
				inputTxt3.id = "tol_score" + i;
				inputTxt3.onchange = function()
				{
					score_distribute(this.id.substring(9, 11));//传i
				};

				newdiv.appendChild(parttip);
				newdiv.appendChild(type);
				newdiv.appendChild(inputTxt1);

				newdiv.appendChild(tol_num);
				newdiv.appendChild(inputTxt2);

				newdiv.appendChild(tol_score);
				newdiv.appendChild(inputTxt3);

				var wtspc = document.createElement("wtspc");//回车换行
				wtspc.innerHTML = "<br/>";
				wtspc.style.display = "block";
				newdiv.appendChild(wtspc);

				document.getElementById(id).appendChild(newdiv);
			}
		}
	}

	function score_distribute(part_index)//小题分数
	{
		var type = document.getElementById("type" + part_index).value;
		var tol_num = document.getElementById("tol_num" + part_index).value;
		var tol_score = document.getElementById("tol_score" + part_index).value;

		if (isNaN(tol_num))
		{
			alert("题数应为数字！请重新输入。");
			document.getElementById("tol_num" + part_index).value = "";
		} else if (isNaN(tol_score))
		{
			alert("总分值应为数字！请重新输入。");
			document.getElementById("tol_score" + part_index).value = "";
		} else if ((type != "") && (tol_num != "") && (tol_score != ""))
		{

			var partid = "part_div" + part_index;
			
			var minpartnum = document.getElementById(partid)
					.getElementsByTagName("score_div").length;
			for ( var k = 1; k <= minpartnum; k++)
			{
				document.getElementById(partid).removeChild(
						document.getElementById("score" + k).parentNode);
						document.getElementById(partid).removeChild(document.getElementById("wtspc"+part_index));
			}

			var tip = document.createElement("span");//往页面动态插入html元素的方法!!
			tip.innerHTML = "第" + part_index + "大题各小题分值：";
			tip.style.display = "block";

			var newdiv = document.createElement("score_div");
			newdiv.id = "score_div" + part_index;
			for ( var i = 1; i <= tol_num; i++)
			{
				var score_tip = document.createTextNode(part_index + "." + i
						+ "题分值：");
				var inputtext = document.createElement("input");
				inputtext.type = "text";
				inputtext.name = "score";
				inputtext.id = "score" + i;
				newdiv.appendChild(score_tip);
				newdiv.appendChild(inputtext);
			}
			var wtspc = document.createElement("wtspc");//回车换行
			wtspc.innerHTML = "<br/><br/><br/>";
			wtspc.id = "wtspc" + part_index;
			wtspc.style.display = "block";
			//document.getElementById(id).appendChild(tip);
			document.getElementById(partid).appendChild(newdiv);
			document.getElementById(partid).appendChild(wtspc);//插入回车换行符
		}
	}

	function createmodel()
	{
		var subject = document.model.subject.value;
		var total_problems = document.model.total_problems.value;
		var total_score = document.model.total_score.value;
		//var str='{"course_name":"'+subject+'","count":'+total_problems+',"score":'+total_score+',"part":[';
		var str = '{course_name : subject , count : total_problems , score:total_score';

		if (total_problems != 0)//若大题数不为0
		{
			str += ',part:[';
			for ( var part_index = 1; part_index <= total_problems; part_index++)//处理每大题
			{
				var type = document.getElementById("type" + part_index).value;
				var tol_num = document.getElementById("tol_num" + part_index).value;
				var tol_score = document.getElementById("tol_score"
						+ part_index).value;
				str += '{type :  type , count : tol_num , score : tol_score';

				if (tol_num != 0)//若每道大题的小题数不为0
				{
					str += ',question_score:[';
					for ( var score_index = 1; score_index <= tol_num; score_index++)//处理每大题里的小题
					{
						var score = document.getElementById("score"
								+ score_index).value;
						if (score_index != tol_num)
						{
							str += 'score,';
						} else
							str += 'score ]';
					}
				}
				if (part_index != total_problems)
				{
					str += '},';//一道大题处理完，非末道
				} else
					str += '}';//一道大题处理完，末道
			}
			str += ']';//大题全拼完
		}
		str += '}';//全部拼完
		var obj = eval('(' + str + ')');
		var result = JSON.stringify(obj);
		//alert("result : " + result);
		window.location = "servlet/DocGenerator?jsonStr="+result;
	}
</script>
</head>

<body bgcolor="white">
	<form name="model" action="servlet/DocGenerator" method="post">
		<!--可以以盒子模型对之界面加工-->
		学科名称：<input name="subject" type="text" onchange="detail_problem()" /> <br />
		大题总数：<input name="total_problems" type="text" onchange="detail_problem()" /><br />
		试卷总分：<input name="total_score" type="text" onchange="detail_problem()" /><br />
		<br>
		<table width=60>
			<tr>
				<td>
					<div id="list"></div> <input type="button" name="sure" value="生成Word文档"
					onclick="createmodel()" />
				<td />
			<tr />
		</table>
	</form>
</body>
</html>